<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib uri="/WEB-INF/tld/security.tld" prefix="s" %>

<fmt:setBundle basename="messages"/>
<script type="text/javascript" language="javascript">
	var validSubmit = true;

	function submitAddCostElement(form){
		var transportShortName;
		var transportDescription;
		var cornType;		
		var transportCapacity;
		var transportPriority;
		var transportNomenclature;
		
		if (validSubmit){
			transportShortName = document.getElementById('transportShortName').value;
			transportDescription = document.getElementById('transportDescription').value;
			cornTypesList = document.getElementById('cornTypeId').value;
			transportCapacity = document.getElementById('transportCapacity').value;
			transportPriority = document.getElementById('transportPriority').value;
			transportNomenclature = document.getElementById('transportNomenclature').value;

			transportCapacity = transportCapacity.replace(/,/g, "");
			
			document.getElementById('trnCode').value = transportShortName;
			document.getElementById('trnDescription').value = transportDescription;
			document.getElementById('cornTypeId').value = cornTypesList;
			document.getElementById('trnCapacity').value = transportCapacity;
			document.getElementById('trnPriority').value = transportPriority;
			document.getElementById('trnNomenclature').value = transportNomenclature;
			
			validSubmit = false;
			
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitSaveEditTransport(form, flag){
		var transportCapacity;
		if (validSubmit){

			if (validateEditTransportForm(form)){
				transportCapacity = document.getElementById('transportCapacity').value;
				
				document.getElementById('transportCapacity').value = transportCapacity.replace(/,/g, "");
				
				document.getElementById('createNewTransport').value = flag;
				
				validSubmit = false;

				form.submit();				
			}
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}
	
	function submitDeleteTransportCostElement(form, transportCostElementIndex, shortName){

		document.getElementById('transportCostElementIndex').value = transportCostElementIndex;
		var transportShortName;
		var transportDescription;
		var transportCapacity;
		var transportPriority;
		var transportNomenclature;
		if(confirmDelete(shortName)){
			
			if (validSubmit){
				transportShortName = document.getElementById('transportShortName').value;
				transportDescription = document.getElementById('transportDescription').value;
				transportCapacity = document.getElementById('transportCapacity').value;
				transportPriority = document.getElementById('transportPriority').value;
				transportNomenclature = document.getElementById('transportNomenclature').value;
				
				transportCapacity = transportCapacity.replace(/,/g, "");
				
				document.getElementById('trnDelCode').value = transportShortName;
				document.getElementById('trnDelDescription').value = transportDescription;
				document.getElementById('trnDelCapacity').value = transportCapacity;
				document.getElementById('trnDelPriority').value = transportPriority;
				document.getElementById('trnDelNomenclature').value = transportNomenclature;
				
				validSubmit = false;
				form.submit();
			} else {
				alert('<fmt:message key="warning.message.common.processing"/>');
			}
		}
	}
	
	function submitCancel(form){
		if (validSubmit){
			validSubmit = false;
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function validateEditTransportForm(form){
		var validTransportForm = false;
		var transportShortName = "";
		var transportDescription = "";
		var transportCapacity = "";
		var transportPriority = "";
		var transportNomenclature = "";
		
		transportShortName = document.getElementById('transportShortName').value;
		transportDescription = document.getElementById('transportDescription').value;
		transportCapacity = document.getElementById('transportCapacity').value;
		transportPriority = document.getElementById('transportPriority').value;
		transportNomenclature = document.getElementById('transportNomenclature').value;
		
		transportShortName = trim(transportShortName);
		transportDescription = trim(transportDescription);
		transportNomenclature = trim(transportNomenclature);
		transportCapacity = transportCapacity.replace(/,/g, "");
		
		if (transportShortName.length == 0){
			alert('<fmt:message key="warning.message.transports.required.code"/>');
		} else if (transportDescription == 0){
			alert('<fmt:message key="warning.message.transports.required.description"/>');
		} else if(isNaN(parseFloat(transportCapacity)) || parseFloat(transportCapacity) == 0.0  || parseFloat(transportCapacity) > 99999.999){
			alert('<fmt:message key="warning.message.transports.invalid.capacity"/>');
		} else if (isNaN(parseInt(transportPriority)) || parseInt(transportPriority) == 0){
			alert('<fmt:message key="warning.message.transports.invalid.priority"/>');
		} else if(transportNomenclature.length == 0){
			alert('<fmt:message key="warning.message.transports.invalid.nomenclature"/>');
		} else {
			validTransportForm = true;
		}
		
		return validTransportForm;
	}
</script>
<table width="100%" align="center" border="0">
	<tr>
		<td style="width: 100%" align="center">
			<form name="EditTransportForm" action="saveEditTransport.do" method="POST">
				<input type="hidden" name="createNewTransport" id="createNewTransport"/>
				<input type="hidden" name="id" id="id" value="<c:out value="${transportView.transportIdentifier}"/>"/>
				<table style="width: 340" align="center" border="0" cellspacing="1" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna">
							<fmt:message key="label.transports.edit.editTransport"/>
						</th>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.query.transport"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" style="text-transform: uppercase;" name="code" id="transportShortName" value="<c:out value="${transportView.transportShortName}"/>" onkeydown="return noPaste(event, this);" size="15" maxlength="5" onfocus="this.select();" class="campo_texto"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.query.description"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="description" id="transportDescription" value="<c:out value="${transportView.transportDescription}"/>" onkeydown="return noPaste(event, this);" size="31" maxlength="50" onfocus="this.select();" class="campo_texto"/>
						</td>
					</tr>					
					<tr>
					<td class="etiqueta" style="width: 35%">
						<fmt:message key="label.common.corntype"/>
					</td>									
						<td class="etiqueta_campo">
							<logic:notEmpty name="cornTypesList">
								<select id="cornTypeId" name="cornTypeId" style="width:185px;" class="campo_texto">							
									<c:forEach items="${cornTypesList}" var="record">
										<c:choose>	
											<c:when test="${record.cornTypeId == transportView.cornTypesView.cornTypeId}">
												<option value="<c:out value="${record.cornTypeId}" />"  selected="selected"><c:out value="${record.code}"/> - <c:out value="${record.description}"/></option>																																				
											</c:when>
											<c:otherwise>
												<option value="<c:out value="${record.cornTypeId}" />"  ><c:out value="${record.code}"/> - <c:out value="${record.description}"/></option>																																													
											</c:otherwise>	
										</c:choose>	
									</c:forEach>
								</select>
							</logic:notEmpty>
						</td>
					</tr>				
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.query.nomenclature"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="nomenclature" id="transportNomenclature" onfocus="this.select();" value="<c:out value="${transportView.transportNomenclature}"/>" onkeydown="return noPaste(event, this);" size="15" maxlength="2" class="campo_texto"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.query.capacity"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="capacity" id="transportCapacity" value="<fmt:formatNumber maxFractionDigits="3" pattern="###,###,##0.000" value="${transportView.transportCapacity}"/>" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this, 3,false);" size="15" maxlength="10" onfocus="this.select();" class="campo_texto_r"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.query.priority"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="priority" id="transportPriority" value="<c:out value="${transportView.transportPriority}"/>" onkeydown="return (numeric(event, 0) && noPaste(event, this));" size="15" onfocus="this.select();" maxlength="2" class="campo_texto_r"/>
						</td>
					</tr>
				</table>				
			</form>
		</td>
	</tr>
	<tr>
		<td height="10">
		</td>
	</tr>
	<s:entidad nombreCortoMod="CATALOG" nombreCortoSub="TRANS" nombreCortoEnt="COSTLINK" permiso="ADMINISTRACION">
		<tr>
			<td width="100%">
				<form name="DeleteCostElementForm" action="deleteTransportCostElement.do" method="POST">
					<input type="hidden" name="costElementIndex" id="transportCostElementIndex"/>
					<input type="hidden" id="trnDelId" name="id" value="<c:out value="${transportView.transportIdentifier}"/>"/>
					<input type="hidden" id="trnDelCode" name="code"/>
					<input type="hidden" id="trnDelDescription" name="description"/>
					<input type="hidden" id="trnDelCapacity" name="capacity"/>
					<input type="hidden" id="trnDelPriority" name="priority"/>
					<input type="hidden" id="trnDelNomenclature" name="nomenclature"/>
					<table style="width: 340" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
						<tr>
							<td colspan="4" align="center" class="titulo_lista_interna"><B><bean:message key="label.transports.cost.element"/></B></td>
						</tr>
						<tr>
							<td class="titulocolumna" style="text-align: center; width: 10%">
								<a href="#>" onclick="submitAddCostElement(CostElementForm);">
									<img alt="<fmt:message key="common.add"/>" src="images/nuevo.gif" border="0">
								</a>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 40%">
								<fmt:message key="label.transports.costelements.code"/>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 40%">
								<fmt:message key="label.transports.costelements.amount"/>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 10%">
							</td>
						</tr>
						<c:forEach items="${transportView.transportCostElements}" var="transportCostElement" varStatus="status">
							<tr class="<c:choose><c:when test="${status.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
								<td/>
								<td style="text-align: center; vertical-align: top">
									<c:out value="${transportCostElement.chargeTypeShortName}"/>
								</td>
								<td style="text-align: right; vertical-align: top">
									<fmt:formatNumber maxFractionDigits="4" pattern="###,###,##0.0000" value="${transportCostElement.chargeTypeCost}"/>
								</td>
								<td style="text-align: center; vertical-align: top;">
								
									<a href="#" onclick="submitDeleteTransportCostElement(DeleteCostElementForm, '<c:out value="${status.index}"/>', '<c:out value="${transportCostElement.chargeTypeShortName}"/>')">
										<img alt="<fmt:message key="common.delete"/>" src="images/eliminar.jpg" border="0">
									</a>
								</td>
							</tr>
						</c:forEach>
					</table>
				</form>
			</td>
		</tr>
	</s:entidad>
	<tr>
		<td height="10">
		</td>
	</tr>
	<tr>
		<td width="100%">
			<table align="center" width="340" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
				<tr>
					<td style="text-align: center; vertical-align: bottom;" class="columnadatopar">
						<input type="button" value="<fmt:message key="label.transports.add.saveTransport"/>" onclick="submitSaveEditTransport(EditTransportForm);" class="boton"/>
						<input type="button" value="<fmt:message key="label.common.cancel"/>" onclick="submitCancel(CancelEditTransportForm);" class="boton"/>&nbsp;
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<form name="CostElementForm" action="showAddTransportCostElement.do" method="POST">
	<input type="hidden" id="trnId" name="id" value="<c:out value="${transportView.transportIdentifier}"/>"/>
	<input type="hidden" id="trnCode" name="code"/>
	<input type="hidden" id="trnDescription" name="description"/>
	<input type="hidden" id="trnCapacity" name="capacity"/>
	<input type="hidden" id="trnPriority" name="priority"/>
	<input type="hidden" id="trnNomenclature" name="nomenclature"/>
</form>
<form name="CancelEditTransportForm" action="showTransportQuery.do" method="POST">
</form>
<script>

</script>